﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ProjScan.Modules;
using ProjScan.RecordManager;
using ProjScan.RecordManager.AssetData;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.ScanRule
{
	// Token: 0x020001A5 RID: 421
	internal class RecordRuleImp<TAssetData, TManager> : RecordRule where TAssetData : AssetDataBase where TManager : RecordManagerBase<TAssetData>, new()
	{
		// Token: 0x1700034D RID: 845
		// (get) Token: 0x06000E3E RID: 3646 RVA: 0x00007184 File Offset: 0x00005384
		protected virtual bool UseAssetDataCSVHeader
		{
			get
			{
				return true;
			}
		}

		// Token: 0x1700034E RID: 846
		// (get) Token: 0x06000E3F RID: 3647 RVA: 0x00007184 File Offset: 0x00005384
		public virtual bool HasWhiteList
		{
			get
			{
				return true;
			}
		}

		// Token: 0x06000E40 RID: 3648 RVA: 0x00040BA4 File Offset: 0x0003EDA4
		public RecordRuleImp()
		{
			IRecordManager recordManager = RecordManagerFactory.Instance.GetManager<TManager>();
			this.manager = (recordManager as TManager);
			bool flag = this.manager == null;
			if (flag)
			{
				throw new Exception("Manager type not match: " + ((recordManager.GetType() == null) ? null : recordManager.GetType().ToString()) + " v " + ((typeof(TManager) == null) ? null : typeof(TManager).ToString()));
			}
			this.TableTag = recordManager.TableTag;
		}

		// Token: 0x06000E41 RID: 3649 RVA: 0x00040C4C File Offset: 0x0003EE4C
		public override void Init()
		{
			base.Init();
			bool flag = this.SuccessRecords == null;
			if (flag)
			{
				this.SuccessRecords = new List<int>();
			}
			else
			{
				this.SuccessRecords.Clear();
			}
		}

		// Token: 0x06000E42 RID: 3650 RVA: 0x00040C8C File Offset: 0x0003EE8C
		public override void Check(AssetDataBase assetData)
		{
			TAssetData tassetData = assetData as TAssetData;
			string text = null;
			try
			{
				bool flag = this.HasFailedInfos();
				if (flag)
				{
					bool flag2 = !this.IsOK(tassetData, ref text);
					if (flag2)
					{
						this.FailedRecords.Add(tassetData.id);
						this.FailedInfos.Add(string.IsNullOrEmpty(text) ? "Unknown" : text);
					}
					else
					{
						this.SuccessRecords.Add(tassetData.id);
					}
				}
				else
				{
					bool flag3 = !this.IsOK(tassetData);
					if (flag3)
					{
						this.FailedRecords.Add(tassetData.id);
					}
					else
					{
						this.SuccessRecords.Add(tassetData.id);
					}
				}
			}
			catch (Exception ex)
			{
				ScanLog instance = ScanLog.Instance;
				ScanLog.LogLevel logLevel = ScanLog.LogLevel.EXCEPTION;
				Module module = Module.Unknown;
				string string_ = "GenericRecordRule";
				string[] array = new string[5];
				int num = 0;
				Type type = ex.GetType();
				array[num] = ((type != null) ? type.ToString() : null);
				array[1] = "\n";
				array[2] = ex.Message;
				array[3] = "\n";
				array[4] = ex.StackTrace;
				instance.LogInfo(logLevel, module, string_, string.Concat(array));
			}
		}

		// Token: 0x06000E43 RID: 3651 RVA: 0x00040DCC File Offset: 0x0003EFCC
		public override IRecordManager GetManager()
		{
			return this.manager;
		}

		// Token: 0x06000E44 RID: 3652 RVA: 0x00040DEC File Offset: 0x0003EFEC
		public override void OutputData(bool append = false)
		{
			FileMode mode = append ? FileMode.Append : FileMode.Create;
			using (FileStream fileStream = File.Open(this.OutputPath, mode))
			{
				using (StreamWriter streamWriter = new StreamWriter(fileStream))
				{
					bool flag = !append;
					if (flag)
					{
						string value = "\"Location\",\"ShouldBeFixed\",\"ShouldJoinWhiteList\",\"IsFixed\"";
						streamWriter.WriteLine(value);
					}
					for (int i = 0; i < this.FailedRecords.Count; i++)
					{
						TAssetData tassetData = this.manager.datas[this.FailedRecords[i]];
						Debug.Log("不合格资源路径：" + tassetData.GetAssetPath());
						string arg = tassetData.GetAssetPath();
						bool flag2 = this.HasFailedInfos();
						if (flag2)
						{
							arg = this.FailedInfos[i];
						}
						streamWriter.WriteLine("\"{0}\",true,false,false", arg);
					}
				}
			}
		}

		// Token: 0x06000E45 RID: 3653 RVA: 0x00040F0C File Offset: 0x0003F10C
		public override void CreateNewOutputDetailFile()
		{
			using (FileStream fileStream = File.Open(this.DetailOutputPath, FileMode.Create))
			{
				using (new StreamWriter(fileStream))
				{
				}
			}
		}

		// Token: 0x06000E46 RID: 3654 RVA: 0x00040F68 File Offset: 0x0003F168
		public override void OutputDetailData()
		{
			bool flag = this.FailedRecords.Count == 0;
			if (!flag)
			{
				string value = "Pass,AssetPath," + CSVUtils.TranslateHeader(this.GetDetailCSVHeader());
				bool flag2 = !this.HasWhiteList;
				if (flag2)
				{
					value = "Pass," + CSVUtils.TranslateHeader(this.GetDetailCSVHeader());
				}
				bool flag3 = !string.IsNullOrEmpty(value);
				if (flag3)
				{
					using (FileStream fileStream = File.Open(this.DetailOutputPath, FileMode.Create))
					{
						using (StreamWriter streamWriter = new StreamWriter(fileStream))
						{
							streamWriter.WriteLine(value);
							for (int i = 0; i < this.FailedRecords.Count; i++)
							{
								TAssetData tassetData = this.manager.datas[this.FailedRecords[i]];
								string failedInfo = string.Empty;
								bool flag4 = this.HasFailedInfos();
								if (flag4)
								{
									failedInfo = this.FailedInfos[i];
								}
								string str = string.IsNullOrEmpty(tassetData.GetFileAssetPath()) ? string.Empty : tassetData.GetFileAssetPath();
								string value2 = "0," + ToolsHelper.QuoteString(str) + "," + this.GetDetailInfo(tassetData, failedInfo);
								bool flag5 = !this.HasWhiteList;
								if (flag5)
								{
									value2 = "0," + this.GetDetailInfo(tassetData, failedInfo);
								}
								bool flag6 = !string.IsNullOrEmpty(value2);
								if (flag6)
								{
									streamWriter.WriteLine(value2);
								}
							}
							for (int j = 0; j < this.SuccessRecords.Count; j++)
							{
								TAssetData tassetData2 = this.manager.datas[this.SuccessRecords[j]];
								string str2 = string.IsNullOrEmpty(tassetData2.GetFileAssetPath()) ? string.Empty : tassetData2.GetFileAssetPath();
								string value3 = "1," + ToolsHelper.QuoteString(str2) + "," + this.GetDetailInfo(tassetData2, string.Empty);
								bool flag7 = !this.HasWhiteList;
								if (flag7)
								{
									value3 = "1," + this.GetDetailInfo(tassetData2, string.Empty);
								}
								bool flag8 = !string.IsNullOrEmpty(value3);
								if (flag8)
								{
									streamWriter.WriteLine(value3);
								}
							}
						}
					}
				}
			}
		}

		// Token: 0x06000E47 RID: 3655 RVA: 0x0000A3A0 File Offset: 0x000085A0
		protected virtual bool IsOK(TAssetData assetData)
		{
			return true;
		}

		// Token: 0x06000E48 RID: 3656 RVA: 0x0000A3A0 File Offset: 0x000085A0
		protected virtual bool IsOK(TAssetData assetData, ref string failedInfo)
		{
			return true;
		}

		// Token: 0x06000E49 RID: 3657 RVA: 0x00041230 File Offset: 0x0003F430
		protected virtual string GetDetailCSVHeader()
		{
			bool useAssetDataCSVHeader = this.UseAssetDataCSVHeader;
			if (useAssetDataCSVHeader)
			{
				TAssetData tassetData = default(TAssetData);
				bool flag = this.SuccessRecords.Count > 0;
				if (flag)
				{
					tassetData = this.manager.datas[this.SuccessRecords[0]];
				}
				else
				{
					bool flag2 = this.FailedRecords.Count > 0;
					if (flag2)
					{
						tassetData = this.manager.datas[this.FailedRecords[0]];
					}
				}
				bool flag3 = tassetData != null;
				if (flag3)
				{
					return tassetData.GetCSVHeader();
				}
			}
			return "Name,DirID";
		}

		// Token: 0x06000E4A RID: 3658 RVA: 0x000412F0 File Offset: 0x0003F4F0
		protected virtual string GetDetailInfo(TAssetData assetData, string failedInfo)
		{
			string detailCSVHeader = this.GetDetailCSVHeader();
			string[] array = CSVUtils.ParseCsvRow(detailCSVHeader).ToArray();
			string info = assetData.GetInfo();
			string[] array2 = CSVUtils.ParseCsvRow(info).ToArray();
			string csvheader = assetData.GetCSVHeader();
			string[] array3 = CSVUtils.ParseCsvRow(csvheader).ToArray();
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < array.Length; i++)
			{
				int num = Array.IndexOf<string>(array3, array[i]);
				bool flag = num != -1 && num < array2.Length;
				if (flag)
				{
					string text = array2[num];
					bool flag2 = array[i] == "DirID" && !string.IsNullOrEmpty(text);
					if (flag2)
					{
						string assetPath = AssetPathManager.Instance.GetAssetPath(int.Parse(text));
						bool flag3 = !string.IsNullOrEmpty(assetPath);
						if (flag3)
						{
							text = ToolsHelper.QuoteString(assetPath);
						}
					}
					stringBuilder.Append(text);
					bool flag4 = i < array.Length - 1;
					if (flag4)
					{
						stringBuilder.Append(',');
					}
				}
			}
			return stringBuilder.ToString();
		}

		// Token: 0x040008DA RID: 2266
		public TManager manager;

		// Token: 0x040008DB RID: 2267
		public List<int> SuccessRecords;
	}
}
